//  CG2 ueb_3.1
// S.Shanmugaesan  (1061630)  Michael Antemann (10616753)
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

  struct vector
   {
      float x;
      float y;
      float z;
   }o, r10, r40, r60 ,r30;


double berechneGl1 (float k1, float k4,float theta_1 , float theta_4);
float absv(float x,float y,float z);
int xx=0,yy=0,zz=0;

float theta_1 =30 * M_PI /180 ;
float beta_1  =-100 * M_PI /180; 

double strichBreite=10.0;

double koerper_1=40.0;
double koerper_2=20.0;

double koerper_3=180.0;
double koerper_4=20.0;

double koerper_5=140.0;
double koerper_6=20.0;


void init(void)
{
	glClearColor (0.0, 0.0, 0.0, 0.0);
	glShadeModel (GL_FLAT);

	o.x=0.0;
	o.y=0.0;
	o.z=0.0;

	r10.x=100;
	r10.y=-20;
	r10.z=30;

	r40.x=-140;
	r40.y=10;
	r40.z=30;
}

void display(void)
{
	glClear (GL_COLOR_BUFFER_BIT);
	//Linienstaerke
	glLineWidth(3.0);
	//CUBE
	glPushMatrix();
	glTranslatef(0.0,0.0,-650.0);

	glColor3f(1.0,1.0,1.0);
	glRotatef((GLfloat)xx,1.0,0.0,0.0);
	glRotatef((GLfloat)yy,0.0,1.0,0.0);
	glRotatef((GLfloat)zz,0.0,0.0,1.0);
	glutWireCube(300);
        
	glTranslatef(100.0,-20.0,30.0);
        glRotatef((GLfloat)theta_1,1.0,0.0,0.0);
	glBegin(GL_LINES);
		glColor3f (0.0, 1.0, 0.0);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(koerper_1+koerper_2,0.0,0.0); // y=100
        
	glEnd();
		//strich
	glBegin(GL_LINES);
		glLineWidth(3.5);
		glColor3f (1.0, 0.0, 0.0);
		glVertex3f(-strichBreite,0.0,0.0);
		glVertex3f(strichBreite,0.0,0.0);
	glEnd();



glRotatef((GLfloat)-theta_1,1.0,0.0,0.0);
      glTranslatef(-100.0,+20.0,-30.0);
      glTranslatef(-140,10.0,30.0); 
      glBegin(GL_LINES);
		glColor3f (0.1, 0.5, 0.5);
		glVertex3f(0.0,0.0,0.0);
		glVertex3f(0.0,koerper_6+koerper_5,0.0); // x=
	glEnd();
		//Strich
	glBegin(GL_LINES);
		glColor3f (1.0, 0.0, 0.0);
		glVertex3f(0.0,0.0,-strichBreite);
		glVertex3f(0.0,0.0,strichBreite);
glEnd();
glRotatef((GLfloat)+theta_1,1.0,0.0,0.0);
glTranslatef(+140,-10.0,-30.0); 


glBegin(GL_LINES);
              glColor3f (0.3, 0.4, 0.8);
		glVertex3f(100.0,-20.0+koerper_1+koerper_2,30.0);
                glVertex3f(-140.0,(+10+koerper_6+koerper_5),30.0); 
glEnd();


		glPopMatrix();
	        glutSwapBuffers();// setVisible
/*float i =0.0;
  for(i=0.0 ;i<360;i=i+0.01){
    float x = berechneGl1(absv(0.0,koerper_1+koerper_2,0.0),absv(0.0,koerper_6+koerper_5,0.0),theta_1,i);
 
if(x>-0.1 && x< 0.1)
 printf("\n:X: %f",x);
   }*/
}

void keyboard(unsigned char key, int x, int y) {
	switch (key) {

	case 'x':
			xx = (xx + 5) % 360;
				glutPostRedisplay();
				break;
	case 'y':
			yy = (yy + 5) % 360;
				glutPostRedisplay();
				break;
	case 'z':
			zz = (zz + 5) % 360;
				glutPostRedisplay();
				break;

	case 'X':
			xx = (xx - 5) % 360;
				glutPostRedisplay();
				break;
	case 'Y':
			yy = (yy - 5) % 360;
				glutPostRedisplay();
				break;
	case 'Z':
			zz = (zz - 5) % 360;
				glutPostRedisplay();
				break;

	glutPostRedisplay();
	break;

	case 27:
		exit(0);
		break;
	default:
		break;
	}
}

void reshape(int w, int h)
{
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	//glOrtho(-12.0,12.0,-12.0,12.0,0.0,12.0);
	//glOrtho(-400.0, 400.0, -400.0, 400.0, -400.0, 400.0);
    gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,-5,5);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize (800, 800);
	glutInitWindowPosition (50, 50);
	glutCreateWindow (argv[0]);
	init ();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);

	glutMainLoop();
	return 0;
}


double berechneGl1 (float k1, float k4,float theta_1 , float theta_4)
{
  float r30x,r60x;
  float r30y,r60y;
  float r30z,r60z;
    
  r30x = 0-(100+(k1)*sin(theta_1));
  r30y = 0-(-20+(k1)*cos(theta_1));
  r30z = 0-(30+(k1)*sin(theta_1));
  
  r60x = 0-((-140)+cos(theta_4 * k4));
  r60y = 0-((10)+sin(theta_4 * k4));
  r60y = 0-((30)+sin(theta_4 * k4));
  
  r60x = r60x- r30x;
  r60y = r60y- r30y;
  r60z = r60z- r30z;
  float erg = sqrt( (r60x* r60x+r60y *r60y +r60z* r60z) )-200;
  return erg; 
}

float absv(float x,float y,float z){
  return sqrt(x*x+y*y+z*z);
}
